'==============================================================================
'
'  EJECT.BAS - Eject CD tray
'  Copyright (c) 1998-2001 by WebGeek, Inc.
'
'==============================================================================

$INCLUDE "WIN32API.INC"

GLOBAL w95 AS LONG

FUNCTION EjectMedia(BYVAL drive AS STRING) AS LONG

  LOCAL hDisk   AS LONG
  LOCAL fResult AS LONG
  LOCAL cb      AS LONG
  LOCAL Regs    AS DIOC_REGISTERS
  LOCAL bDisk   AS LONG

  IF w95 THEN
    bDisk = (ASC(UCASE$(drive)) - 65) + 1
    hDisk = CreateFile("\\.\vwin32", 0, 0, BYVAL %NULL, 0, %FILE_FLAG_DELETE_ON_CLOSE, %NULL)

    IF hDisk = %INVALID_HANDLE_VALUE THEN
      EXIT FUNCTION
    END IF

    Regs.regEAX = &H440D  'eject media
    Regs.regEBX = bDisk
    Regs.regECX = &H0849

    fResult = DeviceIoControl(hDisk, %VWIN32_DIOC_DOS_IOCTL, Regs, SIZEOF(Regs), Regs, SIZEOF(Regs), cb, BYVAL %NULL)

    IF (regs.regFlags AND 1) = 1 THEN
      fResult = 0
    END IF
  ELSE
    hDisk = CreateFile("\\.\"+drive, %GENERIC_READ, 0, BYVAL %NULL, %OPEN_EXISTING, 0, %NULL)

    IF hDisk = %INVALID_HANDLE_VALUE THEN
      EXIT FUNCTION
    END IF

    fResult = DeviceIoControl(hDisk, %IOCTL_STORAGE_EJECT_MEDIA, BYVAL %NULL, 0, BYVAL %NULL, 0, cb, BYVAL %NULL)
  END IF

  CloseHandle hDisk

  IF fResult THEN
    FUNCTION = -1
  END IF

END FUNCTION

FUNCTION LoadMedia(BYVAL drive AS STRING) AS LONG

  LOCAL hDisk   AS LONG
  LOCAL fResult AS LONG
  LOCAL cb      AS LONG
  LOCAL Regs    AS DIOC_REGISTERS
  LOCAL bDisk   AS LONG

  IF w95 THEN
    bDisk = (ASC(UCASE$(drive)) - 65) + 1
    hDisk = CreateFile("\\.\vwin32", 0, 0, BYVAL %NULL, 0, %FILE_FLAG_DELETE_ON_CLOSE, %NULL)

    IF hDisk = %INVALID_HANDLE_VALUE THEN
      EXIT FUNCTION
    END IF

    Regs.regEAX = &H440D  'eject media
    Regs.regEBX = bDisk
    Regs.regECX = &H0849

    fResult = DeviceIoControl(hDisk, %VWIN32_DIOC_DOS_IOCTL, Regs, SIZEOF(Regs), Regs, SIZEOF(Regs), cb, BYVAL %NULL)

    IF (regs.regFlags AND 1) = 1 THEN
      fResult = 0
    END IF
  ELSE
    hDisk = CreateFile("\\.\"+drive, %GENERIC_READ, 0, BYVAL %NULL, %OPEN_EXISTING, 0, %NULL)

    IF hDisk = %INVALID_HANDLE_VALUE THEN
      EXIT FUNCTION
    END IF

    fResult = DeviceIoControl(hDisk, %IOCTL_STORAGE_LOAD_MEDIA, BYVAL %NULL, 0, BYVAL %NULL, 0, cb, BYVAL %NULL)
  END IF

  CloseHandle hDisk

  IF fResult THEN
    FUNCTION = -1
  END IF

END FUNCTION


FUNCTION PbMain() AS LONG

  LOCAL drive  AS STRING
  LOCAL ErText AS ASCIIZ * 64
  LOCAL e      AS LONG
  LOCAL vi     AS OSVERSIONINFO
  LOCAL dt     AS LONG
  LOCAL ej     AS LONG

  STDOUT "EJECT v2.02 - Removable Media Eject/Load"
  STDOUT "Copyright (c) 1998-2001 WebGeek, Inc. (dave@basicguru.com)"
  STDOUT ""

  vi.dwOsVersionInfoSize = SIZEOF(vi)
  GetVersionEx vi
  w95 = (vi.dwPlatformId = %VER_PLATFORM_WIN32_WINDOWS)

  IF LEN(COMMAND$) THEN

    drive = UCASE$(LEFT$(COMMAND$, 1) + ":")

    ej = INSTR(LCASE$(COMMAND$), "/l") = 0

  ELSE

    STDOUT "Usage:  eject drive [/l]"
    STDOUT ""
    STDOUT "  /l          load (does not work with all CDROM drives)"
    EXIT FUNCTION

  END IF

  dt = GetDriveType(BYCOPY drive)

  SELECT CASE DT

    CASE %DRIVE_REMOVABLE
      IF ej THEN
        STDOUT "Ejecting Removable drive " & drive;
        e = EjectMedia(drive) AND 1
      ELSE
        STDOUT "Loading Removable drive " & drive;
        e = LoadMedia(drive) AND 1
      END IF
      STDOUT ""

    CASE %DRIVE_CDROM
      IF ej THEN
        STDOUT "Ejecting CDROM drive " & drive;
        e = EjectMedia(drive) AND 1
      ELSE
        STDOUT "Loading CDROM drive " & drive;
        e = LoadMedia(drive) AND 1
      END IF
      STDOUT ""

    CASE ELSE
      STDOUT "Error! Invalid drive type."

  END SELECT

  FUNCTION = e

END FUNCTION
